#import <Foundation/Foundation.h>
#import <objc/runtime.h>

#import "framework/FWCommon.h"

#import "PITask.h"


@implementation PITask

@synthesize dueDate, dueTime, contextClassification, flagged, parent, child, parentKey, importance, priority, action, project, context, percentComplete;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

+ (PITask*)nativeTaskWithKey:(NSString *)newKey title:(NSString *)newTitle icon:(NSString *)newIcon startDate:(NSDate *)newStart dueDate:(NSDate *)newEnd dueTime:(NSDate *)newEndTime calendar:(NSString *)newCalendar calendarIcon:(NSString *)newCalIcon calendarColor:(UIColor *)newCalColor parentKey:(NSString*)newParent
{
    PITask* task = [[[PITask alloc] init] autorelease];
    
    task->key = [newKey copy];
    task->native = YES;
    
    task->title = [newTitle copy];
    task->icon = [newIcon copy];
    
    [task setStartDate:newStart];
    [task setDueDate:newEnd];
    [task setDueTime:newEndTime];
    
    task->calendar = [newCalendar copy];
    task->calendarIcon = [newCalIcon copy];
    task->calendarColor = [newCalColor retain];

    task->parentKey = [newParent copy];
    
    return task;
}

+ (PITask*)piTaskWithKey:(NSString *)newKey title:(NSString *)newTitle icon:(NSString *)newIcon startDate:(NSDate *)newStart dueDate:(NSDate *)newEnd dueTime:(NSDate *)newEndTime calendar:(NSString *)newCalendar calendarIcon:(NSString *)newCalIcon calendarColor:(UIColor *)newCalColor parentKey:(NSString*)newParent
{
    PITask* task = [[[PITask alloc] init] autorelease];
    
    task->key = [newKey copy];
    task->native = NO;
    
    task->title = [newTitle copy];
    task->icon = [newIcon copy];
    
    [task setStartDate:newStart];
    [task setDueDate:newEnd];
    [task setDueTime:newEndTime];
    
    task->calendar = [newCalendar copy];
    task->calendarIcon = [newCalIcon copy];
    task->calendarColor = [newCalColor retain];
    
    task->parentKey = [newParent copy];
    
    return task;
}

- (id)init
{
    self = [super init];
    if (self) {
        
        // Initialization code here.
    }
    
    return self;
}

- (void)dealloc
{
    [dueDate release];
    [dueTime release];
    
    [priority release];

    [project release];
    
    [parentKey release];
    
    [context release];

    [super dealloc];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Object duplication

- (id)copyWithZone:(NSZone *)zone
{
    PITask* copy = [[[self class] allocWithZone:zone] init];
    
    copy->key = [self.key retain];
    copy->native = self.native;
    
    copy->title = [self.title retain];
    copy->icon = [self.icon retain];
    
    copy->contextClassification = self.contextClassification;
    copy->repeating = self.repeating;
    
    [copy setStartDate:self.startDate];
    [copy setDueDate:self.dueDate];
    [copy setDueTime:self.dueTime];
    
    copy->calendar = [self.calendar retain];
    copy->calendarIcon = [self.calendarIcon retain];
    copy->calendarColor = [self.calendarColor retain];

    copy->flagged = self.flagged;
    copy->child = self.child;
    copy->parent = self.parent;
    copy->importance = self.importance;
    copy->action = self.action;
    copy->sensitivity = self.sensitivity;

    [copy setPriority:self.priority];
    [copy setProject:self.project];
    
    copy->parentKey = [self.parentKey retain];
    
    return copy;
}


// ******************************************************************************************
#pragma mark -
#pragma mark Debugging functions

- (NSString*)description
{
    return [NSString stringWithFormat:
            @"<%@: %p;"
            " title = %@;"
            " key = %@;"
            " native = %i;"
            " icon = %@;"
            " startDate = %@;"
            " dueDate = %@;"
            " dueTime = %@;"
            " calendar = %@;"
            " calendarIcon = %@;"
            " calendarColor = %@;"
            " importance = %i;"
            " priority = %@;"
            " action = %i;"
            " repeating = %i;"
            " project = %@;"
            " flagged = %i;"
            " parent = %i;"
            " child = %i;"
            " parentKey = %@;"
            " percentComplete = %i;"
            " context = %@;"
            " tags = %@;"
            " notes = %@>",
            [self class],
            self,
            self.title,
            self.key,
            self.native,
            self.icon,
            self.startDate,
            self.dueDate,
            self.dueTime,
            self.calendar,
            self.calendarIcon,
            self.calendarColor,
            self.importance,
            self.priority,
            self.action,
            self.repeating,
            self.project,
            self.flagged,
            self.parent,
            self.child,
            self.parentKey,
            self.percentComplete,
            self.context,
            self.tags,
            self.notes];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Information access

- (BOOL)hasDetails
{
    return !self.startDate.isNull ||
            !dueDate.isNull ||
            !dueTime.isNull ||
            (priority && (priority.length > 0)) ||
            (project && (project.length > 0));
}

@end
